package com.buymobile.controller;

import java.util.List;

import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.buymobile.domain.Company;
import com.buymobile.domain.Property;
import com.buymobile.domain.Type;
import com.buymobile.domain.Value;
import com.buymobile.service.PropertyService;
import com.buymobile.service.TypeService;
import com.buymobile.service.ValueService;

@Controller
@RequestMapping("/admin/type")
public class TypePropertyValueController {

	private TypeService typeService;
	private PropertyService propertyService;
	private ValueService valueService;
	
	/****************************************************************************/
	/************************type************************************************/
	/****************************************************************************/
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String setAddTypeForm(){
		return "manager/type/add";
	}
	
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public String submitAddType(@ModelAttribute Type type){
		typeService.addType(type);
		return "redirect:/admin/type/list";
	}
	
	@RequestMapping("/list")
	public String listAllType(Model model){
		List<Type> list = typeService.listAllTypes();
		model.addAttribute("list", list);
		return "manager/type/list";
	}
	
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public String editType(@PathVariable("id") Integer id,Model model){
		Type type = typeService.getTypeById(id);
		model.addAttribute("obj", type);
		return "manager/type/edit";
	}

	@RequestMapping(value="/update",method=RequestMethod.POST)
	public String updateType(@ModelAttribute Type type){
		typeService.updateType(type);
		return "redirect:/admin/type/list";
	}
	
	/****************************************************************************/
	/***********************property*********************************************/
	/****************************************************************************/
	
	
	@RequestMapping(value="/{typeId}/add",method=RequestMethod.GET)
	public String setAddPropertyForm(){
		return "manager/property/add";
	}
	
	@RequestMapping(value="/{typeId}/list",method=RequestMethod.GET)
	public String listPropertiesOfType(@PathVariable("typeId") Integer typeId,Model model){
		Type type = typeService.getTypeById(typeId);
		List<Property> properties = propertyService.listPropertiesByTypeId(typeId);
		type.setProperties(properties);
		model.addAttribute("type",type);
		return "manager/property/list";
	}
	
	@RequestMapping(value="/{typeId}/save",method=RequestMethod.POST)
	public String addProperty(@PathVariable("typeId") Integer typeId,Model model,@ModelAttribute Property property){
		Type type = typeService.getTypeById(typeId);
		property.setType(type);
		propertyService.addProperty(property);
		model.addAttribute("type",type);
		return "redirect:/admin/type/" + typeId + "/list";
	}
	
	@RequestMapping(value="/{typeId}/{id}",method=RequestMethod.GET)
	public String editProperty(@PathVariable("id") Integer id,Model model){
		Property property = propertyService.getPropertyById(id);
		model.addAttribute("obj", property);
		return "manager/property/edit";
	}

	@RequestMapping(value="/{typeId}/update",method=RequestMethod.POST)
	public String updateProperty(@PathVariable("typeId") Integer typeId,@ModelAttribute Property property){
		propertyService.updateProperty(property);
		return "redirect:/admin/type/" + typeId + "/list";
	}
	
	/****************************************************************************/
	/***********************value*********************************************/
	/****************************************************************************/
	
	@RequestMapping(value="/property/{propertyId}/add",method=RequestMethod.GET)
	public String setAddValueForm(){
		return "manager/value/add";
	}
	
	/**
	 * 新增属性值
	 * 
	 * @param propertyId
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/property/{propertyId}/list",method=RequestMethod.GET)
	public String listPropertyValues(@PathVariable("propertyId") Integer propertyId,Model model){
		Property property = propertyService.getPropertyById(propertyId);
		Type type = typeService.getTypeById(property.getType().getId());
		property.setType(type);
		List<Value> values = valueService.getValuesByPropertyId(propertyId);
		property.setValues(values);
		model.addAttribute("property",property);
		return "manager/value/list";
	}
	
	@RequestMapping(value="/property/{propertyId}/save",method=RequestMethod.POST)
	public String addPropertyvalue(@PathVariable("propertyId") Integer propertyId,@ModelAttribute Value value){
		Property property = new Property();
		property.setId(propertyId);
		value.setProperty(property);
		valueService.addValue(value);
		return "redirect:/admin/type/property/" + propertyId + "/list";
	}
	
	@RequestMapping(value="/property/{propertyId}/{id}",method=RequestMethod.GET)
	public String editValue(@PathVariable("id") Integer id,Model model){
		Value value = valueService.getValueById(id);
		model.addAttribute("obj", value);
		return "manager/value/edit";
	}

	@RequestMapping(value="/property/{propertyId}/update",method=RequestMethod.POST)
	public String updateValue(@PathVariable("propertyId") Integer propertyId,@ModelAttribute Value value){
		valueService.updateValue(value);
		return "redirect:/admin/type/property/" + propertyId + "/list";
	}
	
	@Autowired
	public void setTypeService(TypeService typeService) {
		this.typeService = typeService;
	}
	
	@Autowired
	public void setPropertyService(PropertyService propertyService) {
		this.propertyService = propertyService;
	}
	
	@Autowired
	public void setValueService(ValueService valueService) {
		this.valueService = valueService;
	}
}
